//
//  UILabel+DataBind.m
//  DataBind
//
//  Created by Super on 2019/12/16.
//  Copyright © 2019 Super. All rights reserved.
//

#import "UILabel+DataBind.h"

#define DataBindViewProps @[@"backgroundColor", @"textColor", @"text"]

@implementation UILabel (DataBind)

- (NSDictionary *)textColorInfo {
    return [self getInfo:@"textColor"];
}

- (NSDictionary *)textInfo {
    return [self getInfo:@"text"];
}

- (NSDictionary *)getInfo:(NSString *)uiBindProp {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self forKey:@"bindUI"];
    [dict setObject:uiBindProp forKey:@"UIBindProp"];
    return [NSDictionary dictionaryWithDictionary:dict];
}

/*----------------update data----------------*/
- (void)updateChange:(NSString *)uiBindProp value:(id)value {
    NSUInteger index = [DataBindViewProps indexOfObject:uiBindProp];
    switch (index) {
        case 0:
            [self updateBackgroundColor:value];
            break;
        case 1:
            [self updateTextColor:value];
            break;
        case 2:
            [self updateText:value];
            break;
        default:
            break;
    }
}

- (void)updateBackgroundColor:(id)value {
    UIColor *color = (UIColor *)value;
    self.backgroundColor = color;
}

- (void)updateTextColor:(id)value {
    UIColor *color = (UIColor *)value;
    self.textColor = color;
}

- (void)updateText:(id)value {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", value]];
    [self setAttributedText:text];
}

- (void)bindKey:(NSString *)key observer:(id)observer {
    [self addObserver:observer forKeyPath:key options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

- (void)unbindKey:(NSString *)key observer:(id)observer {
    [self removeObserver:observer forKeyPath:key];
}

@end
